package com.xaut.outpatientsystem.medicinalManagement.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.xaut.outpatientsystem.medicinalManagement.service.OrderRecordService;
import com.xaut.outpatientsystem.medicinalManagement.service.ReceiptService;
import com.xaut.outpatientsystem.medicinalManagement.util.TestFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/excel")
@RestController
public class excelController {
    @Autowired
    private ReceiptService receiptService;

    @Resource
    private OrderRecordService orderRecordService;

    @RequestMapping("/write")
    public String compositeFill(int id, HttpServletResponse response) throws IOException {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的listint id
        // 获取开药表中药品列表，计算药品的总价
//        double total = 0.;
//        List<XXX> list = xxxService.findByCaseId(id);
//        for (XXX xxx : list) {
//            total += xxx.getPrice() / 100 * xxx.getNums();
//        }
        // 获取xx表对应的实体类，从而得到病人姓名...
//        OtherTable otherTable = xxService.findById(id);
        // File.separator 的作用相当于 ' \ ',在 windows 中 文件文件分隔符 用 ' \ ' 或者 ' / ' 都可以
        // excel模板所在路径
//        String templateFileName = TestFileUtil.getPath() + "demo" + "/" + "template.xlsx";
//        System.out.println(templateFileName);
        // excel输出文件的路径
//        String fileName = TestFileUtil.getPath() + "templateFill" + System.currentTimeMillis() + ".xlsx";
//        System.out.println(fileName);
        // 获取对象，用于写操作
//        ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
//        WriteSheet writeSheet = EasyExcel.writerSheet().build();
//        FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
        // 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1，然后多个list必须用 FillWrapper包裹
//        excelWriter.fill(list, writeSheet);
//        excelWriter.fill(list, writeSheet);
//        Map<String, Object> map = new HashMap<>();
//        map.put("date", "2021年10月9日13:28:28");
//        map.put("name", otherTable.getPatienName());
//        map.put("total", total);
//        excelWriter.fill(map, writeSheet);
//        // 别忘记关闭流
//        excelWriter.finish();
//        System.out.println("akkakkakakk!!");
        // 将收据单信息保存到数据库中
//        Receipt receipt = new Receipt();
//        receipt.setPayee("小刘");
//        receipt.setPatientName(otherTable.getPName());
//        receipt.setCaseId(id);
//        receipt.setTime(new Date());
//        receipt.setTotal(Integer.valueOf(total * 100));
//        receiptService.save(receipt);

        String fileName = "";
        // 定义文件流
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String excelFileName = URLEncoder.encode(fileName, String.valueOf(StandardCharsets.UTF_8)).replaceAll("\\+", "%20");
        // excelFileName: 文件名
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + excelFileName + ".xls");
        OutputStream outputStream = response.getOutputStream();
        // 将id和outputStream文件流传入服务层
        String index = orderRecordService.outInExcelById(id, outputStream);
        return index;
    }
}
